package com.riant.day09.test2;

/**
 * @Classname BankTest
 * @Description TODO
 * @Date 2021/3/31 9:32
 * @Created by 张斌
 */
public class BankTest {
    public static void main(String[] args) {
        Account account=new Account(100,"结婚基金");
        Thread you=new Drawing(account,50,"you");
        Thread girl=new Drawing(account,100,"girl");
        you.start();
        girl.start();
    }
}

class Account{
    private int amount;
    private String name;

    public Account(int amount, String name) {
        this.amount = amount;
        this.name = name;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void transfer(int amount){
        if(this.amount<amount){
            System.out.println("现金不够");
            return;
        }else{
            this.amount-=amount;
            System.out.println("当前账户中还有："+this.amount);

        }
    }
}
class Drawing extends Thread {
    private Account account;
    private int drawingMoney;

    public Drawing(Account account, int drawingMoney,String name) {
        super(name);
        this.account = account;
        this.drawingMoney = drawingMoney;
    }

    @Override
    public void run() {
        if(account.getAmount()<drawingMoney){
            System.out.println("现金不够");
            return;
        }else{
            try{
                Thread.sleep(1000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }

            account.setAmount(account.getAmount()-drawingMoney);
            int nowMoney = drawingMoney;
            System.out.println("当前账户中还有："+account.getAmount());
            System.out.println(this.getName() +"有"+ nowMoney);
        }
    }
}